package tracking;

import java.net.URLDecoder;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import tracking.dto.BaseStatusDto;
import tracking.dto.Devicedto;
import tracking.dto.Trackingdto;
import tracking.dto.Userdto;

import com.google.gson.Gson;

public class Utils {
	public static Userdto getUserdtoFromJson(String jsonData) {
		Userdto dto = null;
		Gson gson = new Gson();
		dto = gson.fromJson(jsonData, Userdto.class);
		return dto;
	}
	
	public static Trackingdto getTrackingtoFromJson(String jsonData) {
		Trackingdto dto = null;
		Gson gson = new Gson();
		dto = gson.fromJson(jsonData, Trackingdto.class);
		return dto;
	}
	
	public static String getString(BaseStatusDto dto) {
		String json = null;
		Gson gson = new Gson();
		json = gson.toJson(dto);
		return json;
	}
	
	public static String getStringFromUserdto(Userdto dto) {
		String json = null;
		Gson gson = new Gson();
		json = gson.toJson(dto);
		return json;
	}
	
	public static String getStringFromTrackingdto(Trackingdto dto) {
		String json = null;
		Gson gson = new Gson();
		json = gson.toJson(dto);
		return json;
	}
	
	public static String decode (String data) throws Exception{
		try {
			return URLDecoder.decode(data, "UTF-8");
		} catch (Exception e) {
			throw e;
		}
	}
	
	public static String getStringFromDevicedto(Devicedto dto) {
		String json = null;
		Gson gson = new Gson();
		json = gson.toJson(dto);
		return json;
	}
	
	public static String getTime(Date date){
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			
			// convert to yyyy/MM/dd hh:mm:ss format
			
			String str = "";
			
			SimpleDateFormat dateFormat = new SimpleDateFormat(LTEConstants.DATEFORMAT, Locale.ENGLISH);
			str = dateFormat.format(calendar.getTime());
//			str = dateFormat.format(date.getTime());
			return str;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getTime(long date){
		try {
//			Calendar calendar = Calendar.getInstance();
//			calendar.setTime(date);
			
			// convert to yyyy/MM/dd hh:mm:ss format
			
			String str = "";
			
			SimpleDateFormat dateFormat = new SimpleDateFormat(LTEConstants.DATEFORMAT, Locale.ENGLISH);
//			str = dateFormat.format(calendar.getTime());
			str = dateFormat.format(date);
			return str;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getTimeUpdateDB(long date){
		try {
			String str = "";
			SimpleDateFormat dateFormat = new SimpleDateFormat(LTEConstants.DATEFORMAT2, Locale.ENGLISH);
			str = dateFormat.format(date);
			return str;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getDetailsId(long date){
		try {
			String str = "";
			SimpleDateFormat dateFormat = new SimpleDateFormat(LTEConstants.DATEFORMAT2, Locale.ENGLISH);
			str = dateFormat.format(date);
			return str;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
